<template>
	<view class="new_linkman">
		<view class="fllow_form">
			<view><text>*</text>客户名称</view>
			<picker @change="changeCus" disabled :value="cusIndex" :range="cuslist">
				<view class="form_right">
					<view>{{customerName ? customerName : '点击选择'}}</view>
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</picker>
		</view>
		<view class="fllow_form">
			<view><text>*</text>客户签约人</view>
			<picker @change="changeContact" mode="selector" :range="contactList" :value="contactIndex" :range-key="'name'">
				<view class="form_right">
					<view>{{contactname ? contactname : '点击选择'}}</view>
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</picker>
		</view>
		<view class="fllow_form">
			<view><text>*</text>公司签约人</view>
			<picker @change="changeStaff" :range-key="'name'" :range="staffList" :value="staffIndex">
				<view class="form_right">
					<view>{{staffname ? staffname : '点击选择'}}</view>
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</picker>
		</view>
		<form-item v-for="(item,index) in listForm" :key="index" :objInfo="item" @changeData="changeOtherData"></form-item>
		
		
		<!-- 审核信息 -->
		<view class="info_head">审核信息</view>
		<view class="affiliation">
			<view class="aff_head"><text>*</text>由谁审核</view>
			<view class="aff_con">
				<view class="aff_item affbox" v-for="(item,index) in remindPeople" :key="index">
					<image :src="BASE_IMG_URL+'close.png'" class="delremind" @click="delRemind(index)"  mode="scaleToFill"></image>
					<image :src="item.img ? item.img :BASE_IMG_URL+ 'headImg.png'"  mode="scaleToFill"></image>
					<view>{{item.name}}</view>
				</view>
				<view class="aff_item aff_last" @click="moreClick">
					<image :src="BASE_IMG_URL+'morebtn.png'"  mode="scaleToFill"></image>
				</view>
			</view>
		</view>
		
		<view class="btn_g bg-blue button-hover round" @click="toSave">
			保存
		</view>
	</view>
</template>

<script>
	import { netPrevCustomer, netLianxiren, netStaffList, netContractDetail, netEditContract } from '@/api/kehu.js'
	import { netSetForm } from '@/api/index.js'
	import formItem from '@/components/form/formitem.vue'
	import { BASE_IMG_URL } from '@/util/api.js'
	export default {
		components:{
			formItem
		},
		data() {
			return {
				id:'',   //合同id
				BASE_IMG_URL:BASE_IMG_URL,
				order_staff_id:'',    //公司签约人
				staffname:'',
				staffList:[],
				staffIndex:null,
				
				customer_id:'',   //客户id
				contacts_id:'',   //客户签约人
				contactname:'',
				contactList:[],
				contactName:'',
				contactIndex:null,
				
				customerlist:[],
				customerName:'',   //客户名称
				cuslist:[],
				cusIndex:null,
				
				dateTime: null,
				dateTimeArray: null,
				startYear: 2000,
				remindPeople:[],   //提醒谁看
				
				info:{},
				listForm:[]
			}
		},
		onLoad(options) {
			this.customer_id = options.kehuid
			this.id = options.id
			//获取客户 列表
			this.getCustomerList()
			//获取  员工列表  公司签约人
			this.getStaffList()
			//获取 合同详情
			this.getContractDetail()
		},
		onShow() {
			this.remindPeople = uni.getStorageSync('remindlist')
		},
		//监听  页面 卸载
		onUnload() {
			uni.removeStorageSync('remindlist')
		},
		methods: {
			//合同详情
			getContractDetail() {
				netContractDetail({id:this.id}).then(res=>{
					console.log(res,'合同 ')
					res = res.data
					this.info = res
					
					this.contacts_id = res.contacts.id
					this.contactname = res.contacts.name
					this.order_staff_id = res.owner_staff.id
					this.staffname = res.owner_staff.name
					//获取  合同 form
					this.getForm()
				})
			},
			getForm() {
				netSetForm({type:'contract'}).then(res=>{
					res = res.data.data
					res.forEach(ele=>{
						ele.value = this.info[ele.id]
					})
					this.listForm = res
					console.log(this.listForm)
				})
			},
			changeOtherData(name,value,obj) {
				let arr = this.listForm
				arr.forEach((ele,index) => {
					if(ele.id == obj.id){
						ele.value = value
						this.$set(this.listForm,index,ele)
					}
				})
			},
			//员工列表
			getStaffList() {
				netStaffList().then(res=>{
					this.staffList = res.data
				})
			},
			getCustomerList () {
				netPrevCustomer().then(res=>{
					this.customerlist = res.data
					this.handleCus(res.data)
				})
			},
			handleCus(data) {
				let arr = []
				data.forEach(ele=>{
					arr.push(ele.name)
				})
				this.cuslist = arr
				this.setDefaultKehu()
			},
			setDefaultKehu() {
				let id = this.customer_id
				this.customerlist.forEach((ele,index)=>{
					if(ele.id == id){
						this.customerName = ele.name
						this.cusIndex = index
					}
				})
				this.getLianxiren()
			},
			changeCus(e) {
				this.cusIndex = e.detail.value
				this.customerName = this.customerlist[e.detail.value].name
				this.customer_id = this.customerlist[e.detail.value].id
				this.getLianxiren()
			},
			getLianxiren() {
				let params = {
					customer_id: this.customer_id
				}
				netLianxiren(params).then(res=>{
					this.contactList = res.data
				})
			},
			//客户签约人
			changeContact(e) {
				this.contactIndex = e.detail.value
				this.contacts_id = this.contactList[e.detail.value].id
				this.contactname = this.contactList[e.detail.value].name
			},
			//公司签约人
			changeStaff(e) {
				this.staffIndex = e.detail.value
				this.order_staff_id = this.staffList[e.detail.value].id
				this.staffname = this.staffList[e.detail.value].name
			},
			//删除 提醒人
			delRemind(index) {
				this.remindPeople.splice(index, 1)
				uni.setStorageSync('remindlist', this.remindPeople)
			},
			moreClick() {
				uni.navigateTo({
					url: '/pagesA/crm/selectMember/selectMember'
				})
			},
			//保存
			toSave() {
				let {
					id,
					order_staff_id,    //公司签约人
					customer_id,   //客户id
					contacts_id,   //客户签约人
					remindPeople,   //提醒谁看
				} = this
				if(!remindPeople || remindPeople.length == 0) {
					uni.showToast({
						title:'请选择审核人',
						icon:'none'
					})
					return
				}
				let arrlist = []
				let params = {}
				this.listForm.forEach(ele=>{
					arrlist.push({
						name:ele.id,
						required: ele.config.required,
						msg: ele.config.placeholder,
						value:ele.value
					})
				})
				for(let i = 0; i < arrlist.length ; i++) {
					if(arrlist[i].required && !arrlist[i].value){
						uni.showToast({
							title: arrlist[i].msg,
							icon:'none'
						})
						return
					}
					params[arrlist[i].name] = arrlist[i].value
				}
				let arr = []
				remindPeople.forEach(ele=>{
					arr.push(ele.id)
				})
				let flow_staff_id = arr.join(',')
				params = Object.assign({},params,{
					id,
					order_staff_id,
					customer_id,
					contacts_id,
					flow_staff_ids:flow_staff_id
				})
				netEditContract(params).then(res=>{
					uni.showToast({
					    title: res.msg,
						icon:'none',
					    duration: 2000
					})
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},2000)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.new_linkman {
	font-size: 28rpx;
	.mar_top {
		margin-top: 30rpx;
	}
	.info_head {
		height: 80rpx;
		line-height: 80rpx;
		font-size: 24rpx;
		color: #999;
		padding-left: 24rpx;
	}
	.fllow_form {
		display: flex;
		justify-content: space-between;
		height: 100rpx;
		line-height: 100rpx;
		background-color: #fff;
		border-bottom: 1rpx solid #EAEAEA;
		padding: 0 24rpx;
		input {
			height: 100rpx;
			line-height: 100rpx;
		}
		.form_right {
			display: flex;
			color: #999;
			image {
				width: 40rpx;
				height: 40rpx;
				margin: 28rpx 5rpx 0 0;
			}
		}
		.theme {
			color: #5ca9fe;
		}
		text {
			color: #f00;
			margin-left: 10rpx;
		}
		.form_input {
			input {
				height: 100rpx;
				line-height: 100rpx;
				text-align: right;
				margin-right: 10rpx;
			}
		}
	}
	.fllow_area {
		background-color: #fff;
		padding: 0 30rpx;
		margin-bottom: 30rpx;
		.area_head {
			height: 100rpx;
			line-height: 100rpx;
			text {
				color: #f00;
			}
		}
		textarea {
			width: 100%;
			height: 300rpx;
			background: #f8f8f8;
		}
	}
}
.affiliation {
		background-color: #fff;
		margin-bottom: 20rpx;
		padding-bottom: 40rpx;
		.aff_head {
			height: 100rpx;
			line-height: 100rpx;
			padding-left: 30rpx;
			text {
				color: #f00;
				margin-left: 10rpx;
			}
		}
		.aff_con {
			display: flex;
			text-align: center;
			flex-wrap: wrap;
		
			.aff_item {
				width: 100rpx;
				margin-right: 15rpx;
		
				image {
					margin-bottom: 15rpx;
					width: 60rpx;
					height: 60rpx;
					border-radius: 50%;
				}
			}
		
			.affbox {
				position: relative;
		
				.delremind {
					width: 32rpx;
					height: 32rpx;
					border-radius: 50%;
					position: absolute;
					right: -16rpx;
					top: -16rpx;
					background: rgba(0, 0, 0, 0.5);
					z-index: 2;
				}
			}
		
			.aff_last {
				image {
					margin-bottom: 15rpx;
					width: 48rpx;
					height: 48rpx;
				}
			}
		}
	}
</style>
